package clientMain;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.border.EtchedBorder;

import clientUtilGUI.UtilityGUI;

public class Loading extends JWindow{
	private JLabel lblLoading;
	private JLabel lblDelay;
	
	public Loading(String delay) {
		JPanel panel = (JPanel) getContentPane();
    	int screenHeight = UtilityGUI.screenSize.height;
    	int screenWidth = UtilityGUI.screenSize.width;
    	setBounds((screenWidth-200)/2, (screenHeight-100)/2, 200, 100);
    	panel.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
        
    	GridBagLayout gbl_panel = new GridBagLayout();
	    gbl_panel.columnWidths = new int[]{0, 0};
	    gbl_panel.rowHeights = new int[]{0, 0, 0};
	    gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
	    gbl_panel.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
	    panel.setLayout(gbl_panel);	    
	    
	    lblLoading = new JLabel();
	    lblLoading.setIcon(new ImageIcon(Loading.class.getResource("/image/ajax-loader.gif")));
	    GridBagConstraints gbc_lblLoading = new GridBagConstraints();
	    gbc_lblLoading.insets = new Insets(0, 0, 5, 0);
	    gbc_lblLoading.gridx = 0;
	    gbc_lblLoading.gridy = 0;
	    panel.add(lblLoading, gbc_lblLoading);
	    
	    lblDelay = new JLabel(delay);
	    lblDelay.setFont(UtilityGUI.fontTblBold);
	    GridBagConstraints gbc_lblDelay = new GridBagConstraints();
	    gbc_lblDelay.insets = new Insets(0, 0, 10, 0);
	    gbc_lblDelay.gridx = 0;
	    gbc_lblDelay.gridy = 1;
	    panel.add(lblDelay, gbc_lblDelay);
	}

	public void open(final int nMilliseconds){
		SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            	Timer timer = new Timer (Integer.MAX_VALUE, new ActionListener() {
            		public void actionPerformed (ActionEvent event) {
            			((Timer) event.getSource()).stop();
            			close();
                    };
                });
            	timer.setInitialDelay (nMilliseconds);
                timer.start();
            	setVisible(true);
            	setAlwaysOnTop(true);
            }
        });
    }
	
	public void close(){
		SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            	setVisible (false);
        	    dispose();
            }
        });
	    
	}
	
	
//	public static void main(String[] args) {
//		Thread thread = new Thread();
//		Loading loading = new Loading("-- Max Intelligent Software --");
//		try {
//			loading.open(10000);
//			thread.sleep(5000);
//			loading.close();
//		} catch (InterruptedException e1) {
//			e1.printStackTrace();
//		}
//	}

	
}
